<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="" prop="titleMenuId">
              <el-input v-model="queryParams.titleMenuId" placeholder="请输入" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="按钮颜色" prop="buttonColor">
              <el-input v-model="queryParams.buttonColor" placeholder="请输入按钮颜色" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="背景颜色" prop="bgColor">
              <el-input v-model="queryParams.bgColor" placeholder="请输入背景颜色" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="是否显示标题" prop="isShowTitle">
              <el-input v-model="queryParams.isShowTitle" placeholder="请输入是否显示标题" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="标题" prop="titleName">
              <el-input v-model="queryParams.titleName" placeholder="请输入标题" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="状态" prop="state">
              <el-input v-model="queryParams.state" placeholder="请输入状态" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="排序索引" prop="sort">
              <el-input v-model="queryParams.sort" placeholder="请输入排序索引" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="背景图片" prop="bgUrl">
              <el-input v-model="queryParams.bgUrl" placeholder="请输入背景图片" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="标题图片" prop="titleImgUrl">
              <el-input v-model="queryParams.titleImgUrl" placeholder="请输入标题图片" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="标题图片链接地址" prop="titleImgLinkUrl">
              <el-input v-model="queryParams.titleImgLinkUrl" placeholder="请输入标题图片链接地址" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="每行显示的数量" prop="columnCount">
              <el-input v-model="queryParams.columnCount" placeholder="请输入每行显示的数量" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="二级分类id" prop="cateChildId">
              <el-input v-model="queryParams.cateChildId" placeholder="请输入二级分类id" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="每次最多显示的数量" prop="showCount">
              <el-input v-model="queryParams.showCount" placeholder="请输入每次最多显示的数量" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="是否现实更多 0 - 不显示  1 - 显示更多" prop="isShowMore">
              <el-input v-model="queryParams.isShowMore" placeholder="请输入是否现实更多 0 - 不显示  1 - 显示更多" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="更多链接" prop="moreLink">
              <el-input v-model="queryParams.moreLink" placeholder="请输入更多链接" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="自定义样式" prop="cssStyle">
              <el-input v-model="queryParams.cssStyle" placeholder="请输入自定义样式" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="只显示当前商家的商品" prop="merId">
              <el-input v-model="queryParams.merId" placeholder="请输入只显示当前商家的商品" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="是否所有城市都可以看" prop="isAllCity">
              <el-input v-model="queryParams.isAllCity" placeholder="请输入是否所有城市都可以看" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="是否开放该功能，关闭时要提示未开放该权限" prop="isOpen">
              <el-input v-model="queryParams.isOpen" placeholder="请输入是否开放该功能，关闭时要提示未开放该权限" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="是否是体验版显示 0 - 正常显示 1 - 体验测试人员显示" prop="isTest">
              <el-input v-model="queryParams.isTest" placeholder="请输入是否是体验版显示 0 - 正常显示 1 - 体验测试人员显示" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" @click="handleAdd" v-hasPermi="['shopping:menuSection:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['shopping:menuSection:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['shopping:menuSection:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" icon="Download" @click="handleExport" v-hasPermi="['shopping:menuSection:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="menuSectionList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="" align="center" prop="titleMenuId" />
        <el-table-column label="内容部分类型
1001 - 轮播图
1002 - 一行单图片商品/优惠券/无链接图片
1003 - 一行多图片商品/优惠券(尺寸一样)
1004 - 一行多图片商品/优惠券(左1右2)
1005 - 一行多图片商品/优惠券(左2右1)
1006 - 跑马灯 (上下的)
1007 - 跑马灯(左右的)
1008 - 1个商品的(带价格销量)
1009 - 多个商品的(带价格销量)" align="center" prop="sectionType" />
        <el-table-column label="按钮颜色" align="center" prop="buttonColor" />
        <el-table-column label="背景颜色" align="center" prop="bgColor" />
        <el-table-column label="是否显示标题" align="center" prop="isShowTitle" />
        <el-table-column label="标题" align="center" prop="titleName" />
        <el-table-column label="状态" align="center" prop="state" />
        <el-table-column label="排序索引" align="center" prop="sort" />
        <el-table-column label="备注信息" align="center" prop="remark" />
        <el-table-column label="背景图片" align="center" prop="bgUrl" />
        <el-table-column label="标题图片" align="center" prop="titleImgUrl" />
        <el-table-column label="标题图片链接地址" align="center" prop="titleImgLinkUrl" />
        <el-table-column label="每行显示的数量" align="center" prop="columnCount" />
        <el-table-column label="结构类型 0 - 默认 1 -上下结构" align="center" prop="columnType" />
        <el-table-column label="二级分类id" align="center" prop="cateChildId" />
        <el-table-column label="显示的商品的类型 0 - 自定义类型" align="center" prop="showType" />
        <el-table-column label="每次最多显示的数量" align="center" prop="showCount" />
        <el-table-column label="是否现实更多 0 - 不显示  1 - 显示更多" align="center" prop="isShowMore" />
        <el-table-column label="更多链接" align="center" prop="moreLink" />
        <el-table-column label="自定义样式" align="center" prop="cssStyle" />
        <el-table-column label="只显示当前商家的商品" align="center" prop="merId" />
        <el-table-column label="是否所有城市都可以看" align="center" prop="isAllCity" />
        <el-table-column label="是否开放该功能，关闭时要提示未开放该权限" align="center" prop="isOpen" />
        <el-table-column label="是否是体验版显示 0 - 正常显示 1 - 体验测试人员显示" align="center" prop="isTest" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['shopping:menuSection:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['shopping:menuSection:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改页面的 内容行  每一行 是一条记录对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="menuSectionFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="" prop="titleMenuId">
          <el-input v-model="form.titleMenuId" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="按钮颜色" prop="buttonColor">
          <el-input v-model="form.buttonColor" placeholder="请输入按钮颜色" />
        </el-form-item>
        <el-form-item label="背景颜色" prop="bgColor">
          <el-input v-model="form.bgColor" placeholder="请输入背景颜色" />
        </el-form-item>
        <el-form-item label="是否显示标题" prop="isShowTitle">
          <el-input v-model="form.isShowTitle" placeholder="请输入是否显示标题" />
        </el-form-item>
        <el-form-item label="标题" prop="titleName">
          <el-input v-model="form.titleName" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入状态" />
        </el-form-item>
        <el-form-item label="排序索引" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序索引" />
        </el-form-item>
        <el-form-item label="备注信息" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注信息" />
        </el-form-item>
        <el-form-item label="背景图片" prop="bgUrl">
          <el-input v-model="form.bgUrl" placeholder="请输入背景图片" />
        </el-form-item>
        <el-form-item label="标题图片" prop="titleImgUrl">
          <el-input v-model="form.titleImgUrl" placeholder="请输入标题图片" />
        </el-form-item>
        <el-form-item label="标题图片链接地址" prop="titleImgLinkUrl">
          <el-input v-model="form.titleImgLinkUrl" placeholder="请输入标题图片链接地址" />
        </el-form-item>
        <el-form-item label="每行显示的数量" prop="columnCount">
          <el-input v-model="form.columnCount" placeholder="请输入每行显示的数量" />
        </el-form-item>
        <el-form-item label="二级分类id" prop="cateChildId">
          <el-input v-model="form.cateChildId" placeholder="请输入二级分类id" />
        </el-form-item>
        <el-form-item label="每次最多显示的数量" prop="showCount">
          <el-input v-model="form.showCount" placeholder="请输入每次最多显示的数量" />
        </el-form-item>
        <el-form-item label="是否现实更多 0 - 不显示  1 - 显示更多" prop="isShowMore">
          <el-input v-model="form.isShowMore" placeholder="请输入是否现实更多 0 - 不显示  1 - 显示更多" />
        </el-form-item>
        <el-form-item label="更多链接" prop="moreLink">
          <el-input v-model="form.moreLink" placeholder="请输入更多链接" />
        </el-form-item>
        <el-form-item label="自定义样式" prop="cssStyle">
          <el-input v-model="form.cssStyle" placeholder="请输入自定义样式" />
        </el-form-item>
        <el-form-item label="只显示当前商家的商品" prop="merId">
          <el-input v-model="form.merId" placeholder="请输入只显示当前商家的商品" />
        </el-form-item>
        <el-form-item label="是否所有城市都可以看" prop="isAllCity">
          <el-input v-model="form.isAllCity" placeholder="请输入是否所有城市都可以看" />
        </el-form-item>
        <el-form-item label="是否开放该功能，关闭时要提示未开放该权限" prop="isOpen">
          <el-input v-model="form.isOpen" placeholder="请输入是否开放该功能，关闭时要提示未开放该权限" />
        </el-form-item>
        <el-form-item label="是否是体验版显示 0 - 正常显示 1 - 体验测试人员显示" prop="isTest">
          <el-input v-model="form.isTest" placeholder="请输入是否是体验版显示 0 - 正常显示 1 - 体验测试人员显示" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="MenuSection" lang="ts">
import { listMenuSection, getMenuSection, delMenuSection, addMenuSection, updateMenuSection } from '@/api/shopping/titlemenu/section';
import { MenuSectionVO, MenuSectionQuery, MenuSectionForm } from '@/api/shopping/titlemenu/section/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const menuSectionList = ref<MenuSectionVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const menuSectionFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: MenuSectionForm = {
  id: undefined,
  titleMenuId: undefined,
  sectionType: undefined,
  buttonColor: undefined,
  bgColor: undefined,
  isShowTitle: undefined,
  titleName: undefined,
  state: undefined,
  sort: undefined,
  remark: undefined,
  bgUrl: undefined,
  titleImgUrl: undefined,
  titleImgLinkUrl: undefined,
  columnCount: undefined,
  columnType: undefined,
  cateChildId: undefined,
  showType: undefined,
  showCount: undefined,
  isShowMore: undefined,
  moreLink: undefined,
  cssStyle: undefined,
  merId: undefined,
  isAllCity: undefined,
  isOpen: undefined,
  isTest: undefined
}
const data = reactive<PageData<MenuSectionForm, MenuSectionQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    titleMenuId: undefined,
    sectionType: undefined,
    buttonColor: undefined,
    bgColor: undefined,
    isShowTitle: undefined,
    titleName: undefined,
    state: undefined,
    sort: undefined,
    bgUrl: undefined,
    titleImgUrl: undefined,
    titleImgLinkUrl: undefined,
    columnCount: undefined,
    columnType: undefined,
    cateChildId: undefined,
    showType: undefined,
    showCount: undefined,
    isShowMore: undefined,
    moreLink: undefined,
    cssStyle: undefined,
    merId: undefined,
    isAllCity: undefined,
    isOpen: undefined,
    isTest: undefined,
    params: {
    }
  },
  rules: {
    id: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    titleMenuId: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    sectionType: [
      { required: true, message: "内容部分类型
1001 - 轮播图
1002 - 一行单图片商品/优惠券/无链接图片
1003 - 一行多图片商品/优惠券(尺寸一样)
1004 - 一行多图片商品/优惠券(左1右2)
1005 - 一行多图片商品/优惠券(左2右1)
1006 - 跑马灯 (上下的)
1007 - 跑马灯(左右的)
1008 - 1个商品的(带价格销量)
1009 - 多个商品的(带价格销量)不能为空", trigger: "change" }
    ],
    buttonColor: [
      { required: true, message: "按钮颜色不能为空", trigger: "blur" }
    ],
    bgColor: [
      { required: true, message: "背景颜色不能为空", trigger: "blur" }
    ],
    isShowTitle: [
      { required: true, message: "是否显示标题不能为空", trigger: "blur" }
    ],
    titleName: [
      { required: true, message: "标题不能为空", trigger: "blur" }
    ],
    state: [
      { required: true, message: "状态不能为空", trigger: "blur" }
    ],
    sort: [
      { required: true, message: "排序索引不能为空", trigger: "blur" }
    ],
    remark: [
      { required: true, message: "备注信息不能为空", trigger: "blur" }
    ],
    bgUrl: [
      { required: true, message: "背景图片不能为空", trigger: "blur" }
    ],
    titleImgUrl: [
      { required: true, message: "标题图片不能为空", trigger: "blur" }
    ],
    titleImgLinkUrl: [
      { required: true, message: "标题图片链接地址不能为空", trigger: "blur" }
    ],
    columnCount: [
      { required: true, message: "每行显示的数量不能为空", trigger: "blur" }
    ],
    columnType: [
      { required: true, message: "结构类型 0 - 默认 1 -上下结构不能为空", trigger: "change" }
    ],
    cateChildId: [
      { required: true, message: "二级分类id不能为空", trigger: "blur" }
    ],
    showType: [
      { required: true, message: "显示的商品的类型 0 - 自定义类型不能为空", trigger: "change" }
    ],
    showCount: [
      { required: true, message: "每次最多显示的数量不能为空", trigger: "blur" }
    ],
    isShowMore: [
      { required: true, message: "是否现实更多 0 - 不显示  1 - 显示更多不能为空", trigger: "blur" }
    ],
    moreLink: [
      { required: true, message: "更多链接不能为空", trigger: "blur" }
    ],
    cssStyle: [
      { required: true, message: "自定义样式不能为空", trigger: "blur" }
    ],
    merId: [
      { required: true, message: "只显示当前商家的商品不能为空", trigger: "blur" }
    ],
    isAllCity: [
      { required: true, message: "是否所有城市都可以看不能为空", trigger: "blur" }
    ],
    isOpen: [
      { required: true, message: "是否开放该功能，关闭时要提示未开放该权限不能为空", trigger: "blur" }
    ],
    isTest: [
      { required: true, message: "是否是体验版显示 0 - 正常显示 1 - 体验测试人员显示不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询页面的 内容行  每一行 是一条记录列表 */
const getList = async () => {
  loading.value = true;
  const res = await listMenuSection(queryParams.value);
  menuSectionList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  menuSectionFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: MenuSectionVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加页面的 内容行  每一行 是一条记录";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: MenuSectionVO) => {
  reset();
  const _id = row?.id || ids.value[0]
  const res = await getMenuSection(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改页面的 内容行  每一行 是一条记录";
}

/** 提交按钮 */
const submitForm = () => {
  menuSectionFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateMenuSection(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addMenuSection(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("操作成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: MenuSectionVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除页面的 内容行  每一行 是一条记录编号为"' + _ids + '"的数据项？').finally(() => loading.value = false);
  await delMenuSection(_ids);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('shopping/menuSection/export', {
    ...queryParams.value
  }, `menuSection_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
